/**
 * Created by HeartFul on 2014/8/14.
 * description ：公文待办任务列表展示
 */
var task = function () {  // 初始化 页面布局
    $(".dt_warp").remove();
    LeftTime = 1;
    var dt_warp = d3.select("body").append("div").classed("dt_warp", true).style({
        height: "100%",
        "overflow": "hidden"
    });
    var db_bg = dt_warp.append("div").classed("db_bg", true).style({
        height: "100%",

        "overflow": "hidden"
    });
    var datong_header = db_bg.append("div").classed("datong_header", true);
    datong_header.append("h1").html("待办任务").style({
        position: 'relative', top: '-1px'
    });

    var span = db_bg.append("div").classed("span", true).style({
        position: 'relative', top: '-22px', 'text-align': 'center', 'z-index': '99', color: '#6bb5fa', "display": "none"
    });
    db_bg.append("div").classed("datong_con", true).classed("datong_con2", true).style({
        top: '-14px', height: '80%'
    });
    var footer = db_bg.append("div").classed("footer", true).style({
        position: 'absolute', bottom: '0px'
    });
    footer.append("div").classed("f_navone_click", true).attr("type", "button")//.append("span");
    d3.select(".f_navone_click").append("span").style("opacity", 0)
    footer.append("div").classed("f_navtwo", true).attr("type", "button");
    footer.append("div").classed("f_navthree", true).attr("type", "button");
    footer.append("div").classed("f_navfour", true).attr("type", "button");


    //通知中心
    $(".f_navtwo").on(touch_s, function () {
        tzzx();

    });
    // 我的应用
    $(".f_navthree").on(touch_s, function () {
        menu();
    });
    //账号管理
    $(".f_navfour").on(touch_s, function () {
        zhgl();

    });
    onTask();
};
var onTask = function () {   // 获取待办列表并展示
    var XXX = false;
    canRefresh = true;
    $("body").css("height", window.innerHeight + "px");
    if (window.localStorage.task && window.localStorage.task != "0")
        d3.select(".f_navone_click span").html(window.localStorage.task);
    else
        d3.select(".f_navone_clcik span").remove();
    try {
        var ws = new CommonWebSocket({  //同步函数 ，使pc端和手机端数据同步
            url: "cmcp/todoListener",
            afterGetMessageFun: function () {
                if (XXX)
                    return;
                if (canRefresh) {
                    setTimeout(function () {
                        reload(true);
                        XXX = false;
                    }, 2000);
                    XXX = true;
                    window.localStorage.status = "false";
                }

            }
        });
    }
    catch (e) {

    }


    if (!window.Ready) {
        Ready();// 打开文件系统
    }

    reload(false);
    window.setInterval(function () {
        if (window.localStorage.status === "true") {
            window.localStorage.status = "false";
            reload(true);
            //console.log(window.localStorage.status);
        }
    }, 2000);

    //获取待办列表数据，并显示
    function reload(flag) {
        var url = serverURL + "cmcp/todo/findTodos";
        if (!flag) {
            PostCP(url, null, function (r, a, b) {
                //   console.log(r, a, b)

                if (!r.length) {
                    Prompt("您好，您暂时没有待办任务", 1000);
                    window.plugins.getIntent.hide();
                }
                window.plugins.getIntent.hide();

                list(r);
            }, function () {
                window.plugins.getIntent.hide();
                Alert("获取数据失败");
            });
        }
        else {
            PostCP(url, null, function (r, a, b) {
                //   console.log(r, a, b)

                if (!r.length) {
                    Prompt("您好，您暂时没有待办任务", 1000);
                    window.plugins.getIntent.hide();
                }
                window.plugins.getIntent.hide();

                list(r);
            }, function () {
                window.plugins.getIntent.hide();
                Alert("获取数据失败");
            }, function () {
            }, function () {
            });
        }


    }
    var getRelationId = function (url) {
        var reLen = url.split("&");
        var tempSTR = ""
        for (var i = 0; i < reLen.length; i++) {
            if (reLen[i].indexOf("relationId") > -1) {
                tempSTR = reLen[i];
                return tempSTR.split("=")[1]
                break;
            }
        }
        return "";
    };
// 待办列表展示
    function list(data) {

     //   console.log(data);
        if (!data)
            return;
        window.localStorage.task = data.length;
        if (data.length) {
            d3.select(".f_navone_click span").html(window.localStorage.task)
                .style({
                    "opacity": 1
                });
            if (data.length >= 100) {
                d3.select(".f_navone_click span").html("99+")
                    .style({
                        "opacity": 1,
                        "width": "30px"
                    });
            }
        }
        else {
            d3.select(".f_navone_click span").remove();
        }

        d3.selectAll(".content").remove();
        d3.select(".datong_con").style({
            height: (height - 97) + "px"
        })
        var datong_con = d3.select(".datong_con").attr("id", "con").append("ul").classed("content", true);
        d3.select(".content").append("div").style("height", "15px").classed("pullDownLabel", true).style("text-align", "center");
//        d3.select(".datong_con").append("div").classed("zk_h", true);
        var load = d3.select(".zk_h");
        content = d3.select(".content");
        for (var i = 0, j = data.length; i < j; i++) {

            var type = data[i]["appImgUrl"] || "";
            var type_ = type.split(".")[0] || "";
            var typeReal = data[i]["typeReal"] || "";
            var taskName = data[i]["taskName"] || "";
            //    var db_or_dsp = "dsp";
            var speed = data[i]["speed"] || "";
            var id = data[i]["businessId"] || "";
            var taskId = data[i]["taskId"] || "";
            var userId = data[i]["userId"] || '';
            var appName = data[i]["appName"] || "";
            var theFlag = data[i]["theFlag"] || 0;
            var businessType = data[i]["businessType"] || "";
            var businessDataId = data[i]["businessDataId"] || "";//edit by lixuefang 20160411,修改businessId为businessDataId
            var todoId = data[i]["todoId"] || "";
            var relationId = getRelationId(data[i]["url"]);
            var mobileUrl =  data[i]["mobileUrl"] || "";//edit by lixuefang 20151224,新公文管理页面跳转
            var roleCode =  data[i]["roleCode"] || "";//edit by lixuefang 20151230,新公文管理页面跳转时所需参数
            if (businessType == "0040") {
                speed = "0004";
                type = "gzjb.png"
            }
          //edit by lixuefang 20151224,新公文管理页面跳转
            var record = content.append("li").attr("class", "li").style("position", "relative").attr("id", id).attr("taskId", taskId).attr("typeReal", typeReal)
                .attr("taskName", taskName).attr("type", type_).attr("businessType", businessType)
                .attr("appName", appName).attr("businessDataId", businessDataId).attr("relationId",relationId)
                .attr("url", data[i]["url"]).attr("userId", userId).attr("todoId", todoId).attr("mobileUrl", mobileUrl).attr("roleCode", roleCode);//edit by lixuefang 20160411,修改businessId为businessDataId
            var p = record.append("p").classed("list_ico", true);
            if(mobileUrl == "oacloud"){
            	var img = p.append("img").attr("src", serverURL + type).style({
                    width: "60px",
                    position: "absolute",
                    top: "10%",
                    height: "60px"
                });
            }else{
            	var img = p.append("img").attr("src", "../../common/page/images/" + type).style({
                    width: "60px",
                    position: "absolute",
                    top: "10%",
                    height: "60px"
                });
            }
            
            var db_record = record.append("dl").style({
                left: "75px"

            }).attr("id", id).attr("taskId", taskId).attr("typeReal", typeReal)
                .attr("taskName", taskName).attr("type", type_);
            var background = {
                l0001: "#ff1111",
                l0002: "#ff1a1a",
                l0003: "#ff900d",
                l0004: "#424141"
            };
            var txt = {
                l0001: "特提",
                l0002: "特急",
                l0003: "急",
                10004: ""
            };
//            var title = data[i]["title"].length >= 10 ? (data[i]["title"].substr(0, 10) + "..") : data[i]["title"];
            var title = handleLongString_(data[i]["title"], 20, "..");
            if (txt["l" + speed] && txt["l" + speed].length) {
                title += "(" + txt["l" + speed] + ")";
            }
            db_record.append("dt").style("color", background["l" + speed])
                .html(title).attr("title", data[i]["title"]);
            db_record.append("dd").html("[" + appName + "]  " + data[i]["time"]).style({
                color: "#999"
            });
            d3.selectAll(".content dt").on(touch_s, function () {
                $(this).parent().parent().css("background-color", "#e2e2e2");
            });
            d3.selectAll(".content dt").on(touch_e, function () {
                $(this).parent().parent().css("background-color", "");
            });
            // 列表点击事件
            d3.selectAll(".content li").on("click", function () {
                if (Flag)
                    return;
                Flag = true;
                setTimeout(function () {
                    Flag = false;
                }, 1000);
                $(this).css("background-color", "");
                var test = $(".content").css("-webkit-transform");
                var render = d3.select(".dt_warp");
                var config = {
                    type: $(this).attr("type"),
                    id: $(this).attr("id"),
                    businessType: $(this).attr("businessType"),
                    taskId: $(this).attr("taskId"),
                    businessDataId: $(this).attr("businessDataId"),//edit by lixuefang 20160411,修改businessId为businessDataId
                    info: $(this).attr("db_or_dsp"),
                    userId: $(this).attr("userId"),
                    roleId: $(this).attr("roleCode"),
                    typeReal: $(this).attr("typeReal"),
                    taskName: $(this).attr("taskName"),
                    url: $(this).attr("url"),
                    screenindex: 1,
                    render: render.node()
                };
                //edit by lixuefang 20151224,新公文管理页面跳转
                if("oacloud" == $(this).attr("mobileUrl")){
                	 var href = location.href;
                     var len = href.lastIndexOf("/");
                     var url = href.substr(0, len - 4) + "xgwgl/xgwgl.html?caseKey="+config.businessType+"&roleId="+config.roleId+"&caseInstanceId="+config.id+"&taskId="+config.taskId;//edit by lixuefang 20160411,修改businessId为businessDataId
                     window.openViewB(url);
                }else{
	                if ("0032" == $(this).attr("businessType")) {
	                    Alert("请于电脑端操作");
	                    return;
	                }
	                else if ("0040" == $(this).attr("businessType")) {
	                    config["todoId"] = $(this).attr("todoId");
	                    var ml = new Clickmed.gzjb(config);
	
	                }
	                else if ("0017" == $(this).attr("businessType")) {
	                    var ml = new Clickmed.dongtai(config);
	                }
	                else if ("0018" == $(this).attr("businessType") || "0019" == $(this).attr("businessType")) {
	                    config.url = "cmcp/super/getSuperVise";
	
	                    var ml = new Clickmed.dubandan(config);
	                }
	                else if ("0024" == $(this).attr("businessType")) {
	                    config.url = "cmcp/super/getBranchSuper"
	                    config["relationId"] = $(this).attr("relationId");
	                    var ml = new Clickmed.dubandan(config);
	                } else if ("0022" == $(this).attr("businessType")) {
	                    var ml = new Clickmed.huizong(config);
	                }
	                else if ("0026" == $(this).attr("businessType")) {
	
	                    var ml = new Clickmed.ysyb(config);
	                }
	                else if ("0029" == $(this).attr("businessType")) {
	
	                    var ml = new Clickmed.ysyb(config);
	                }
	                else if ("0042" == $(this).attr("businessType")) {
	
	                    var ml = new Clickmed.sxfp(config);
	                }
					else if ("0044" == $(this).attr("businessType")) {
	                    var ml = new Clickmed.dbbg(config);
	                }
					else if ("0045" == $(this).attr("businessType")) {
	                    var ml = new Clickmed.dbbg(config);
	                }
					else if ("0046" == $(this).attr("businessType")) {
	                    var ml = new Clickmed.mryl(config);
	                }
	                else if ("hygl" == $(this).attr("type")) {
	                    var ml = new Clickmed.huiyi(config);
	                }
	                else if ("dcdb" == $(this).attr("type")) {
	                    var ml = new Clickmed.overLook(config);
	                }
	                else {
	                    var ml = new Clickmed.DaiBan(config);
	                }
	                ml.init();
                }
            });
        }
        ;
// 列表添加滑动效果
        myScroll = new iScroll("con", {hScrollbar: false, vScrollbar: false, onScrollMove: function () {
            //   return;

            if (this.y > 10) {
                d3.select(".pullDownLabel").html('下拉重新加载...');
            }
            if (this.y > 40) {
                d3.select(".pullDownLabel").html('松手即可加载...');
            }
        },
            onScrollEnd: function () {
                if (d3.select(".pullDownLabel").html() == "松手即可加载...") {
                    reload();
                }
                d3.select(".pullDownLabel").html('');
            }
        });
        $(".datong_con").css({
            "display": "block"
        });
        //  myScroll.scrollToElement('li:nth-child(10)', 100)
        myScroll.refresh();

    }



}